/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: wangxin
 * Date: 2024-09-07
 * Time: 9:39
 */
 class Animal {
    public String name;
    public int age;
    public Animal(String name,int age){
        this.name = name;
        this.age = age;
    }
    public void eat(){
        System.out.println(name + "正在吃饭");
    }
}

class Dog extends Animal{
     public Dog(String name,int age){
         super(name,age);
     }
    @Override
    public void eat(){
        System.out.println(name +"正在吃狗粮！！");
    }
    public void bark(){
        System.out.println(name + "汪汪叫！！！");
    }
}
class Cat extends Animal{
    public Cat(String name, int age) {
        super(name, age);
    }

    @Override
    public void eat(){
        System.out.println(name +"正在吃猫粮！！");
    }
    public void mew(){
        System.out.println(name + "喵喵叫！！！");
    }
}
public class test2 {
    public static void main(String[] args) {
        Dog dog = new Dog("小黄", 12);
        Cat cat = new Cat("小花", 13);

        // 向上转型
        Animal animal = cat;
        animal.eat();
        animal = dog;
        animal.eat();
        if (animal instanceof Cat) {
            cat = (Cat) animal;
            cat.mew();
        }
        if (animal instanceof Dog) {
            dog = (Dog) animal;
            dog.bark();
        }
    }
}
//public class test2{
//     // 方法传参：形参为父类型引用，可以接收任意子类的对象
//     public static void eatFood( Animal animal){
//         animal.eat();
//     }
//    //作返回值：返回任意子类对象
//    public static Animal buyAnimal(String var){
//        if("狗".equals(var) ){
//            return new Dog("狗狗",1);
//        }else if("猫" .equals(var)){
//            return new Cat("猫猫", 1);
//        }else{
//            return null;
//        }
//     }
//    public static void main(String[] args) {
//        Animal dog = new Dog("小黄",12);//直接赋值：子类对象赋值给父类对象
//        Cat cat = new Cat("小花",13);
//       eatFood(dog);
//        System.out.println("/////////////////////////");
//        eatFood(cat);
//    }
//}